/*
*ControlBar
*chenjie
*2012-5-3
*/
package com.player
{
	import com.data.Data;
	import com.event.PlayerEvent;
	
	import fl.controls.Slider;
	import fl.events.SliderEvent;
	import fl.video.SoundEvent;
	import fl.video.VideoEvent;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class ControlBar extends Sprite
	{
		public var playPauseButton:Sprite
		public var bufferingBar:Sprite
		public var seekBar:Sprite
		public var volumeBar:Sprite
		public var fullScreenButton:Sprite
		
		public var progress:progressbg
		
		public var loadingProgress:SeekBarProgress
		public var soundBtn:SoundBtn
		public var shareBtn:ShareBtn
		public var muteButton:MuteButton
		public var timeBar:TextField
		private var textFormat:TextFormat=new TextFormat("",12,0xffffff)
			
		public var bigPlayBtn:PlayerBigBtn=new PlayerBigBtn()	
//		private var bigPauseBtn:PauseBigBtn=new PauseBigBtn()	
			
		private var controlPlayer:MainFlvPlayer	
		
		private var normalSeekBarWidth:int=0
		private var swidth:int = 600;
		private var slider:Slider = new Slider();
		
		private var sliderIsDowning:Boolean=false
			
		private var bg:ControlBarBg=new ControlBarBg()	
		private var volumLine:ControlBarLine=new ControlBarLine()	
			
		private var volumTemp:Number	
		
		private var volumTip:TipMc	
		private var volumText:TextField=new TextField()	
		private var volumTextFormat:TextFormat=new TextFormat("",11,0xffffff,null,null,null,null,null,"center")	
		public function ControlBar()
		{
			super();
			addChild(bg)
			bg.y=5
			playPauseButton=new PlayPauseButton()
			
			bufferingBar=new BufferingBar()	
			bufferingBar.y=15
//			
			seekBar=new SeekBar()
//			seekBar.y=15
			
			timeBar=new TextField()
			timeBar.defaultTextFormat=textFormat
			timeBar.width=100
			timeBar.mouseEnabled=false
			timeBar.y=8
				
			volumeBar=new VolumeBar()
			volumeBar.y=7
			addChild(volumLine)
			
			
			fullScreenButton=new FullScreenButton()
			fullScreenButton.y=8
			
			bigPlayBtn.x=10
			bigPlayBtn.y=-100
//			bigPauseBtn.x=10
//			bigPauseBtn.y=-100
			bigPlayBtn.visible=false
			bigPlayBtn.addEventListener(MouseEvent.CLICK,doPlay)
			addChild(bigPlayBtn)
//			addChild(bigPauseBtn)
			
			addChild(playPauseButton)
			addChild(bufferingBar)
			//addChild(seekBar)
			addChild(slider)
			slider.maximum = 600
			addChild(timeBar)
			addChild(volumeBar)
			addChild(fullScreenButton)
			/*******************************/
			
			loadingProgress=new SeekBarProgress()
			loadingProgress.mouseEnabled=false
			loadingProgress.height=8
//			loadingProgress.y=-2
			slider.addChild(loadingProgress)
				
			progress=new progressbg()
			progress.mouseEnabled = false;
			progress.height=10
			progress.y=-2
			slider.addChild(progress)
				
			slider.swapChildren(progress,slider.getChildAt(1))
			slider.swapChildren(progress,loadingProgress)
				
			muteButton=new MuteButton()
//			soundBtn=new SoundBtn()
//			addChild(soundBtn)
			muteButton.y=8
			addChild(muteButton)
//			soundBtn.gotoAndStop(1)	
//			soundBtn.addEventListener(MouseEvent.CLICK,doSoundClick)
				
			shareBtn=new ShareBtn()
			addChild(shareBtn)
			shareBtn.y=8
			shareBtn.addEventListener(MouseEvent.CLICK,doShareClick)
			/******************************/
			slider.addEventListener(SliderEvent.CHANGE,changeFun);
			slider.addEventListener(SliderEvent.THUMB_PRESS,onMouseDown)
			slider.addEventListener(SliderEvent.THUMB_RELEASE,onMouseUp)
//			slider.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
//			slider.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
//			this.height=100
				
			volumTip=new TipMc()
			volumText.width=volumTip.width
			volumTip.addChild(volumText)
			volumText.defaultTextFormat=volumTextFormat
			volumTip.y=-24
			volumTip.visible=false
			addChild(volumTip)	
//			volumeBar.addChild(volumTip)	
			volumTip.addEventListener(MouseEvent.MOUSE_OUT,doOut)
		}
		
		private function soundUpdate(e:SoundEvent):void{
			volumTip.visible=true
			volumText.text=int(controlPlayer.volume*100)+"%"
			volumTip.x=volumeBar.x+volumeBar.width*controlPlayer.volume-25
			trace("move  "+controlPlayer.volume)
//			if(parent)
//			parent["text"].text="move  "+controlPlayer.volume
//			volumTip.visible=false
		}
		
		private function doOut(e:MouseEvent):void{
			trace("volumTip.visible  ")
			if(parent)	
				parent["text"].text="volumTip.visible "
			volumTip.visible=false
		}
		
		
		private function onMouseDown(e:SliderEvent):void{
			sliderIsDowning=true
				trace("move")
//			bigPlayBtn.visible=false	
		}
		private function onMouseUp(e:SliderEvent):void{
			sliderIsDowning=false
//			if()	
//			bigPlayBtn.visible=true	
		}
		
		private function changeFun(e:SliderEvent):void{
//			controlPlayer.pause()
			controlPlayer.seekPercent(slider.value/swidth*100)
		}
//		public function doSoundClick(e:MouseEvent):void{
//		    if(soundBtn.currentFrame==1){
//				soundBtn.gotoAndStop(2)
//				controlPlayer.changeVolume(0)
//			}else if(soundBtn.currentFrame==2){
//				soundBtn.gotoAndStop(1)
//				controlPlayer.changeVolume(1)
//			}
//		}
		
		public function doPlay(e:MouseEvent):void{
			dispatchEvent(new PlayerEvent(PlayerEvent.PLAY))
		}
		
		public function doShareClick(e:MouseEvent):void{
			dispatchEvent(new PlayerEvent(PlayerEvent.SHOW_SHARE))
		}
		
		public function changeWH(width:int,height:int):void{
//			volumeBar.visible=false
//			this.width=width
//			var volume:Number=controlPlayer.volume
			y=height-33
			swidth = width;
			updateNormal()
			bg.width=swidth
//				
			volumLine.y=8
			volumLine.x=volumeBar.x+volumeBar.width+5
			slider.maximum = swidth-10
			slider.width = swidth-10
			MovieClip(volumeBar).gotoAndStop(2)
				
			controlPlayer.changeVolume(0)
			controlPlayer.changeVolume(1)
				
			volumTip.visible=false
				
//				
//			controlPlayer.volume=0.5
//			parent["text"].text="@@ "+volumeBar.width	
//			volumeBar.visible=true
		}
		
		public function changeState(name:String,stage:Stage,player:MainFlvPlayer):void{
			
			if(controlPlayer==null)controlPlayer=player
			controlPlayer.addEventListener(SoundEvent.SOUND_UPDATE,soundUpdate)
//			progress.x=2
			switch(name){
				case "fullScreen":
					y=stage.fullScreenHeight-33
					x=0
					swidth = stage.fullScreenWidth
					updateFullScreen(stage)
					bg.width=swidth
					break
				case "normal":
					y=Data.playerHeight-33
					if(as3_player.mode == as3_player.SWF){
//						y=Data.swfHeight-207
					}
					swidth = Data.playerWidth;
					updateNormal()
					bg.width=swidth
					break
			}
			volumLine.y=8
			volumLine.x=volumeBar.x+volumeBar.width+5
			slider.maximum = swidth-10
			slider.width = swidth-10
		}
		
		public function updateNormal():void{
				
			playPauseButton.x=0
				
			bufferingBar.x=32+10
			//seekBar.x=bufferingBar.x
//			seekBar.y=-15
//			seekBar.scaleX = swidth/330
			slider.x=0
			slider.y = 1;
			slider.width = swidth-10;
			progress.width = swidth-10;
//			timeBar.x=350
			timeBar.x=36
				
//			shareBtn.x=450
//			muteButton.x=490
//			volumeBar.x=520
//			fullScreenButton.x=575
				
			shareBtn.x=swidth-200
			fullScreenButton.x=swidth-30
			muteButton.x=swidth-150
			volumeBar.x=swidth-100
		
			if(normalSeekBarWidth==0){
				//normalSeekBarWidth=seekBar.width
			}else{
				//seekBar.width=normalSeekBarWidth
			}
			
		}
		
		public function updateFullScreen(stage:Stage):void{
			
//			playPauseButton.x=0
//			
//			bufferingBar.x=32+10
//			seekBar.x=bufferingBar.x
//			timeBar.x=360
//			
//				
//			shareBtn.x=stage.fullScreenWidth-200
//			fullScreenButton.x=stage.fullScreenWidth-30
//			soundBtn.x=stage.fullScreenWidth-150
//			volumeBar.x=stage.fullScreenWidth-100
			
			playPauseButton.x=0
			
			bufferingBar.x=5
			slider.x=0
			slider.y=1
//			seekBar.x=5
//			seekBar.y=-15
//			seekBar.scaleX = stage.fullScreenWidth/330	
//			seekBar.width=stage.fullScreenWidth-100
			//controlPlayer.pause()
			//controlPlayer.play();
			//controlPlayer.play();
			timeBar.x=36
			
//			timeBar.x=stage.fullScreenWidth-300
			shareBtn.x=stage.fullScreenWidth-200
			fullScreenButton.x=stage.fullScreenWidth-30
			muteButton.x=stage.fullScreenWidth-150
			volumeBar.x=stage.fullScreenWidth-100
				
//			seekBar.scaleX=4
//			SeekBar(seekBar).progress_mc.width=stage.fullScreenWidth-300-timeBar.width-20
//			SeekBar(seekBar).seekBarHandle.width=stage.fullScreenWidth-300-timeBar.width-20
				
			SeekBar(seekBar).addFrameScript(1,frame)
		}
		
		public function frame():void{
//			SeekBar(seekBar).handleLinkageID = "SeekBarHandle";
//			SeekBar(seekBar).handleLeftMargin = 2;
//			SeekBar(seekBar).handleRightMargin = 2;
//			SeekBar(seekBar).handleY = 20;
		}
		
		public function updateTime(num:Number):void{
//			if(volumTemp != controlPlayer.volume){
//				volumTemp=controlPlayer.volume
//			}else{
//			  volumTip.visible=false
//			}
//			trace("@@  "+controlPlayer.state)
//		  progress.width=(swidth-15)*controlPlayer.playheadPercentage/100
			progress.width=slider.value
		  loadingProgress.width=swidth*(controlPlayer.bytesLoaded/controlPlayer.bytesTotal)
		  if(controlPlayer.playheadPercentage && sliderIsDowning == false)	
			  slider.value = ((swidth)*controlPlayer.playheadPercentage/100); 
		}
		
		
		
	}
}